<?php

//新闻管理
if (!defined('IN_T')) {
    die('hacking attempt');
}

//查看新闻列表
if ($act == "index") {
    $size = 15;
    $page = intval($_REQUEST['page']);
    $page = $page < 1 ? 1 : $page;
    $title = Common::sfilter($_REQUEST['title']);
    $res = news_list($title, $page, $size);
    $tp->assign('res', $res);
    $pages = Common::set_page($size, $page, $res['count']);
    // var_dump($pages);
    foreach ($pages as $key => $value) {
        $pages[$key]['url'] = "/" . COMPANY_PATH . "/?m=news&page=" . $value['num'] . $res['spm'];
    }
    $tp->assign("page", $page);
    $tp->assign("pages", $pages);
} //新闻编辑
else if ($act == "detail") {
    $id = intval($_REQUEST['id']);
    $news = $Db->query("SELECT * FROM " . $Base->table('company_news') . " WHERE id=$id AND c_id = " . $GLOBALS['company']['c_id'] . " ", "Row");
    //判断操作合法性
    if (empty($news)) {
        $id = 0;
    }
    //显示界面
    if (empty($_POST)) {
        $detail_type = $news["new_type"] == 0 ? "new" : "plus";//该文章是发文还是追加
        //获取所有分类
        $cat_types = productCatList();
        $tp->assign('cat_types', $cat_types);
        if ($id > 0) {
            //读取他的子新闻
            $sql = "select * from " . $Base->table('company_news') . " where new_type=" . $id . " and c_id=" . $GLOBALS['company']['c_id'] . " ";
            $childNews = $Db->query($sql);
            $tp->assign('childNews', $childNews);
        }
        //读取所有一级新闻
        $sql = "select * from " . $Base->table('company_news') . " where new_type=0";
        $allNews = $Db->query($sql);
        $tp->assign('allNews', $allNews);
        $tp->assign('news', $news);
        $tp->assign('detail_type',$detail_type);
    } //数据处理
    else {
        $res['status'] = 0;
        $new_type = intval($_POST['new_type']);//新闻子分类 0 父级
        $detail_type =  Common::sfilter($_POST['pub_type']);//追加还是发文
        if($detail_type == "new"){
            $new_type = 0;
        }
        $data = array(
            'c_id' => $company['c_id'],
            'title' => Common::sfilter($_POST['title']),
            'content' => Common::sfilter($_POST['content'], 'html'),
            'sort' => Common::sfilter($_POST['sort']),
            'href' => Common::sfilter($_POST['href']),
            'cat_type' => intval(Common::sfilter($_POST['cat_type'])),
            'new_type' => $new_type
        );
        if ($detail_type == "plus") {
            //删除追加不要的信息
            $data["title"] = "";
            $data['href'] = "";
        }
        if ($detail_type == "new" && empty($data['title'])) {
            $res['msg'] = '新闻标题不能为空！';
        } else if (($detail_type == "plus" || empty($data["href"])) && empty($data['content'])) {
            $res['msg'] = '新闻内容不能为空！';
        } else if (empty($data['sort'])) {
            $res['msg'] = '排序不能为空！';
        } else if ($detail_type == "new" && $data['cat_type'] < 0) {
            $res['msg'] = '分类不能为空！';
        } else {
            if($detail_type == "plus"){
                //追加时读取父新闻分类,保持一致
                $sql = "select cat_type from ".$Base->table('company_news')." where id=".$data["new_type"];
                $cat_type = $Db->query($sql,"Row");
                $data["cat_type"] = $cat_type["cat_type"];
            }
            if ($news) {
                //替换
                $Db->update($Base->table('company_news'), $data, array('id' => $id));
            } else {
                $Db->insert($Base->table('company_news'), $data);
            }
            $res = array('status' => '1', 'msg' => '提交成功', 'href' => '/' . COMPANY_PATH . '/?m=news');
        }
        echo $Json->encode($res);
        exit;
    }

} //新闻删除
else if ($act == 'delete') {
    $id = intval($_POST['id']);
    $res['status'] = 0;
    $Db->beginTransaction();
    try {
        //删除顶级新闻
        $Db->execSql("delete from " . $Base->table('company_news') . " where id=$id and c_id = " . $GLOBALS['company']['c_id'] . "");
        //删除顶级新闻的评论
        $Db->execSql("delete from " . $Base->table('company_new_comment') . " where new_id = $id");
        //删除子新闻
        $Db->execSql("delete from " . $Base->table('company_news') . " where new_type=$id and c_id=" . $GLOBALS['company']['c_id']);
        $res['status'] = 1;
        $Db->commit();
    } catch (Exception $e) {
        $Db->rollback();
    }
    echo $Json->encode($res);
    exit;
}
$tp->assign('act', $act);
$tp->assign('nav', '新闻动态');

function news_list($title, $page, $size)
{
    $spm = '';
    $sql = "SELECT news.*,admin.name cat_name FROM " . $GLOBALS['Base']->table('company_news') . " as news " .
        "left join " . $GLOBALS['Base']->table('company_new_cat') . " as cat on cat.id=news.cat_type and cat.c_id = news.c_id " .
        "left join " . $GLOBALS['Base']->table('company_admin_new_cat') . ' as admin on admin.id=cat.pid ' .
        "WHERE news.c_id = " . $GLOBALS['company']['c_id'] . " and news.new_type=0 ";

    if ($title) {
        $sql .= " AND title LIKE '%$title%' ";
        $spm .= "&title=$title";
        $GLOBALS['tp']->assign('search', $title);
    }
    // echo $sql;
    $rescount = count($GLOBALS['Db']->query($sql));
    $sql .= " ORDER BY sort  LIMIT " . ($page - 1) * $size . ", " . $size . "";

    $list = $GLOBALS['Db']->query($sql);
    foreach ($list as $k => $value){
        //读取他的子文章
        $sql = "SELECT news.*,admin.name cat_name FROM " . $GLOBALS['Base']->table('company_news') . " as news " .
            "left join " . $GLOBALS['Base']->table('company_new_cat') . " as cat on cat.id=news.cat_type and cat.c_id = news.c_id " .
            "left join " . $GLOBALS['Base']->table('company_admin_new_cat') . ' as admin on admin.id=cat.pid ' .
            "WHERE news.c_id = " . $GLOBALS['company']['c_id'] . " and news.new_type=".$value['id'];
        $child = $GLOBALS['Db']->query($sql);
        foreach ($child as $c_k => $c_val){
            $child[$c_k]["content"] = mb_substr(Common::sfilter($c_val["content"]),0,30);
        }
        $list[$k]["child"] = $child;
    }
    return array('list' => $list, 'count' => $rescount, 'spm' => $spm);
}

function productCatList()
{
    $sql = "SELECT new.*,admin.name,admin.icon FROM " . $GLOBALS['Base']->table('company_new_cat') . " as new " .
        'left join' . $GLOBALS['Base']->table('company_admin_new_cat') . ' as admin on admin.id=new.pid ' .
        "ORDER BY  sort asc , id asc";
    $list = $GLOBALS['Db']->query($sql);
    return $list;
}


?>